﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DATN.mds.Product
{
    public partial class EditProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                Load_data();
                
            }
            Button2.Attributes.Add("onclick", "return confirm('Bạn có chắc chắn xóa không?');");

        }

        protected void Load_data()
        {
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
            cnn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("Select * From Products Where Pro_Id = " + Convert.ToInt32(Session["Pro_Id"].ToString()), cnn);

            da.Fill(ds, "Products");

            //TextBox1.Text = ds.Tables["Notes"].Rows[0].ItemArray[0].ToString();
            TextBox1.Text = ds.Tables["Products"].Rows[0].ItemArray[1].ToString();
            TextBox2.Text = ds.Tables["Products"].Rows[0].ItemArray[2].ToString();
            double pri = double.Parse(ds.Tables["Products"].Rows[0].ItemArray[3].ToString());
            //TextBox3.Text = string.Format("{0:0,0}", pri);
            TextBox3.Text = string.Format("{0:0,0}", pri);
            //string date = ds.Tables["Notes"].Rows[0].ItemArray[3].ToString();

            ds.Dispose();
            da.Dispose();
            cnn.Close();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();

            string com = "Delete From Products Where Pro_Id = " + Convert.ToInt32(Session["Pro_Id"].ToString());

            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = com;

            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();

            Page.Response.Redirect("~/mds.Product/ProductIndext.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string name = TextBox1.Text;
                string descrip = TextBox2.Text;
                double price = double.Parse(TextBox3.Text);

                SqlConnection cnn = new SqlConnection();
                cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();

                string com1 = "Update Products "
                    + "\n Set Name = N'" + name + "',"
                    + "Describe = N'" + descrip + "',"
                    + "Price = " +"'"+ price+ "'"
                    + "\n Where Pro_Id =" + Convert.ToInt32(Session["Pro_Id"].ToString());

                SqlCommand cmd = cnn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = com1;

                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();

                Page.Response.Redirect("~/mds.Product/ProductIndext.aspx");
            }
        }

    }
}